package com.xbongbong.paas.toolbox.util;

import com.xbongbong.paas.constant.PaasConstant;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/12/9 15:40
 * @since v1.0
 */
public class MathUtil {

    private static final String SCIENTIFIC_NOTATION = "E";

    /**
     * 取整或者四舍五入
     * 如果accuracy为0，则表示取整，即截取小数点前面的位数；
     * 如果accuracy不为0，则按照四舍五入规则运行
     * @param value 数字类型数据
     * @param accuracy 精度
     * @return
     */
    public static BigDecimal ceil(Object value, Integer accuracy) {
        if (!(value instanceof Number)) {
            return null;
        }
        if (Objects.isNull(accuracy)) {
            accuracy = PaasConstant.DEFAULT_ACCURACY;
        }
        Number number = (Number) value;
        BigDecimal bigDecimal = new BigDecimal(number.doubleValue());
        BigDecimal resultBig = bigDecimal.setScale(accuracy, BigDecimal.ROUND_HALF_UP);
        return resultBig;
    }
    /**
     * 获取科学计数法的小数位数
     * @param num
     * @return java.lang.String
     * @author 吴峰
     * @date 2020/11/13 9:29
     * @throws
     */
    public static String getPattern(String num) {
        String pattern = "0.000000";
        if (num.contains(SCIENTIFIC_NOTATION)) {
            String[] numArray = num.split(SCIENTIFIC_NOTATION);

            int index = Integer.valueOf(numArray[1]) + numArray[0].replace(".", "").length();
            String value = "";
            for (int i = 0; i < index; i++) {
                value = value + "0";
            }
            if (StringUtil.isNotEmpty(value)) {
                pattern = "0." + value;
            }
        }
        return pattern;
    }

    /**
     * 提供精确的小数位四舍五入处理。
     * @param v 需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(double v,int scale){
        if(scale<0){
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(v);
        BigDecimal one = new BigDecimal("1");
        return b.divide(one,scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 将科学技术法转成数字字符串
     * @param value
     * @return
     */
    public static Object scientificNotation(Object value) {
        if (Objects.isNull(value)) {
            return null;
        }
        if (value.toString().contains(SCIENTIFIC_NOTATION)) {
            // 解开科学计数法
            String num = String.valueOf(value);
            String pattern = getPattern(num);
            DecimalFormat format = new DecimalFormat(pattern);
            return format.format(value);
        }
        return value;
    }

    /**
     * 获取科学计数法的值，上面scientificNotation这个方法不适用，所以才重新写了一个
     *
     * @param
     * @return
     */
    public static String getScientificNotationValue(String str){
        String value = str;
        BigDecimal bd = new BigDecimal(str);
        value = String.valueOf(Long.parseLong(bd.toPlainString()));
        return value;
    }

    public static void main(String[] args) {
//        System.out.println(ceil(-0.00000000005820766091346741, 2));
//        System.out.println(scientificNotation(-0.00000000005820766091346741));
//        System.out.println(scientificNotation(19971400000000L));


        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 13; i++) {
            list.add(i);
        }
        int num = list.size()%2 == 0 ? list.size()/2 : list.size()/2 + 1;
        int page = 0;
        int pageSize = 2;
        for (int i = 0; i < num; i++) {
            if (pageSize >= list.size()) {
                pageSize = list.size();
            }
            List<Integer> newList = list.subList(page, pageSize);
            page = pageSize;
            pageSize = pageSize + 2;
            System.out.println(i + "====" +newList);
        }
    }
}
